<?php

namespace Admin\Models\school;

use Admin\Models\BaseModel;
use Helpers\User;

class XHSchoolVisit extends BaseModel
{


    /**
     *
     * @var integer
     */
    public $iId;

    /**
     *
     * @var integer
     */
    public $iSchoolId;

    /**
     *
     * @var integer
     */
    public $iSchoolCount;

    /**
     *
     * @var integer
     */
    public $iPeopleCount;

    /**
     *
     * @var integer
     */
    public $iStarttime;

    /**
     *
     * @var integer
     */
    public $iEndtime;

    /**
     *
     * @var string
     */
    public $mESchedule;

    /**
     *
     * @var string
     */
    public $mEPhotos;

    /**
     *
     * @var string
     */
    public $sRemarks;

    /**
     *
     * @var integer
     */
    public $iStatus;

    /**
     *
     * @var integer
     */
    public $bDelete;

    /**
     *
     * @var integer
     */
    public $iCreateUserId;

    /**
     *
     * @var string
     */
    public $tCreateDate;

    /**
     *
     * @var integer
     */
    public $iLastModifyUserId;

    /**
     *
     * @var string
     */
    public $tLastModifyDate;

    /**
     * Initialize method for model.
     */
    public function initialize()
    {
        $this->useDynamicUpdate(true);
        $this->setSchema("xh_webmanage");
        $this->setSource("XHSchool_Visit");
        $this->hasMany(
            'iId',
            'Admin\Models\school\XHSchoolVisitType',
            'iSVid'
        );
        $this->hasMany(
            'iId',
            'Admin\Models\school\XHSchoolVisitRecord',
            'iSVid'
        );
    }

    /**
     * Returns table name mapped in the model.
     *
     * @return string
     */
    public function getSource()
    {
        return 'XHSchool_Visit';
    }

    /**
     * Allows to query a set of records that match the specified conditions
     *
     * @param mixed $parameters
     * @return XHSchoolVisit[]|XHSchoolVisit|\Phalcon\Mvc\Model\ResultSetInterface
     */
    public static function find($parameters = null)
    {
        return parent::find($parameters);
    }

    /**
     * Allows to query the first record that match the specified conditions
     *
     * @param mixed $parameters
     * @return XHSchoolVisit|\Phalcon\Mvc\Model\ResultInterface
     */
    public static function findFirst($parameters = null)
    {
        return parent::findFirst($parameters);
    }

    /**
     *
     */
    public function beforeCreate()
    {
        $userId  = User::getId();
        $time    = date('Y-m-d H:i:s');
        $this->tCreateDate = $this->tCreateDate ?: $time;
        $this->iCreateUserId = $this->iCreateUserId ?: $userId;
        $this->tLastModifyDate = $this->tLastModifyDate ?: $time;
        $this->iLastModifyUserId = $this->iLastModifyUserId ?: $userId;
    }

    /**
     * @param null $data
     * @param null $whiteList
     * @param bool $recordUpdateInfo
     * @return bool
     */
    public function save($data = null, $whiteList = null, $recordUpdateInfo = true)
    {
        if ($recordUpdateInfo) {
            parent::beforeUpdate();
        } else {
            $this->beforeUpdate();
        }
        parent::save($data, $whiteList);
        return $this->afterSave();
    }


    public function afterSave()
    {
        return $this->iId;
    }

    /**
     * @return bool
     */
    public function beforeUpdate()
    {
        if (property_exists($this, 'tLastModifyDate')) {
                    $this->writeAttribute('tLastModifyDate', date('Y-m-d H:i:s'));
        }
        if (property_exists($this, 'iLastModifyUserId')) {
            $this->writeAttribute('iLastModifyUserId', User::getId());
        }
        return true;
    }

//    public function hasMany($fields, $referenceModel, $referencedFields, $options = null)
//    {
//        parent::hasMany('iId', 'XHSchoolVisitType', 'iSVid', $options);
//        // TODO: Change the autogenerated stub
//    }
}
